05. Exercise: Collections

Exercise: Collections

In this next video, Jeff will walk through an example that uses the Collections interface. We recommend that you open up IntelliJ and follow along on your own, pausing the video as needed. Below the video, you'll find a list of all the key things you should be sure to try before moving on.

Nd079 C1 L5 A03a Collections Exercise

Create an Example Using the Iterable Interface

Task Description:

Be sure to try out an example that uses the Iterable interface:

Task List:

Task Feedback:

Nice work!

Use collections to process and store data

Task Description:

Let's get some practice working with the Collections class to process and store data.

Task List:

Task Feedback:

Nice work!

Solution

    List<String> names = new LinkedList<String>();
    names.add("Mike");
    names.add("Bob");
    names.add("Alice");

    Iterator<String> iterator = names.iterator();

    while(iterator.hasNext()){
      System.out.println(iterator.next());
    }
public class CollectionsExercise {

    public static void main(String[] args) {

        List<String> listOfItems = new LinkedList<String>();
        listOfItems.add("Mike");
        listOfItems.add("Bob");
        listOfItems.add("Alice");

        for (String name : listOfItems) {
            System.out.println(name);
        }
    }
}